package com.example.aiquestions.controller;

import com.example.aiquestions.entity.Subject;
import com.example.aiquestions.model.common.ApiResponse;
import com.example.aiquestions.service.SubjectService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/subjects")
public class SubjectController {
    
    @Autowired
    private SubjectService subjectService;

    /**
     * 获取用户创建的所有学科
     * @param userId 用户id
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页的学科列表
     */
    @GetMapping("/list")
    public ApiResponse<PageInfo<Subject>> getSubjects(
            @RequestParam("userId") Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Subject> pageInfo = subjectService.getSubjectsByUserId(userId, pageNum, pageSize);
        return ApiResponse.success(pageInfo);
    }

    /**
     * 创建学科
     * @param userId 用户id
     * @param name 学科名称
     * @param description 学科描述
     * @return 创建的学科信息
     */
    @PostMapping("/create")
    public ApiResponse<Subject> createSubject(
            @RequestParam("userId") Long userId,
            @RequestParam("name") String name,
            @RequestParam(value = "description", required = false) String description) {
        Subject subject = subjectService.createSubject(userId, name, description);
        return ApiResponse.success(subject);
    }

    /**
     * 更新学科
     * @param id 学科id
     * @param name 学科名称
     * @param description 学科描述
     * @return 更新后的学科信息
     */
    @PostMapping("/update")
    public ApiResponse<Subject> updateSubject(
            @RequestParam("id") Long id,
            @RequestParam("name") String name,
            @RequestParam(value = "description", required = false) String description) {
        Subject subject = subjectService.updateSubject(id, name, description);
        return ApiResponse.success(subject);
    }

    /**
     * 删除学科
     * @param id 学科id
     * @return 操作结果
     */
    @PostMapping("/delete")
    public ApiResponse<Boolean> deleteSubject(@RequestParam("id") Long id) {
        boolean result = subjectService.deleteSubject(id);
        return ApiResponse.success(result);
    }

    /**
     * 获取学科详情
     * @param id 学科id
     * @return 学科信息
     */
    @GetMapping("/detail")
    public ApiResponse<Subject> getSubject(@RequestParam("id") Long id) {
        Subject subject = subjectService.getSubjectById(id);
        return ApiResponse.success(subject);
    }
}
